﻿using System;
using System.Globalization;
using Amazon.S3;
using Amazon.S3.Model;

namespace CoreLibrary.Aws {

    public sealed class S3Util {
        
        public static string NormalizePath(string key) {
            return key.Replace('\\', '/');
        }

        public static DateTime? GetDateModified(S3Response response) {
            return DateTime.SpecifyKind(
                   DateTime.ParseExact(
                        response.Headers.Get("Last-Modified"),
                        "ddd, dd MMM yyyy HH:mm:ss \\G\\M\\T",
                        CultureInfo.InvariantCulture
                   ), 
                   DateTimeKind.Local
                   );
        }

        public static void CopyBucket(AmazonS3 s3, 
                                      string sourceBucket, 
                                      string destinationBucket, 
                                      S3CannedACL cannedAcl) {
            var request = new ListObjectsRequest {
                BucketName = sourceBucket
            };
            using (var response = s3.ListObjects(request)) {
                   response.S3Objects.ForEach(obj => s3.CopyObject(
                       new CopyObjectRequest {
                           SourceBucket      = sourceBucket,
                           SourceKey         = obj.Key,
                           DestinationBucket = destinationBucket,
                           DestinationKey    = obj.Key,
                           CannedACL         = cannedAcl
                       }
                    ));
            }
        }

        public static void ChangeKeys(AmazonS3 s3,
                                      string bucket,
                                      S3CannedACL cannedAcl,
                                      string keyPrefix,
                                      Func<string, string> changeKeyFunc) {

            var request = new ListObjectsRequest {
                BucketName = bucket, 
                Prefix     = keyPrefix
            };

            using (var response = s3.ListObjects(request)) {
                foreach (var obj in response.S3Objects) {
                    if (obj.Key.EndsWith("/")) {
                        continue;
                    }

                    var newKey = changeKeyFunc(obj.Key);
                    if (newKey.Equals(obj.Key)) {
                        continue;
                    }

                    s3.CopyObject(new CopyObjectRequest {
                        SourceBucket      = bucket,
                        SourceKey         = obj.Key,
                        DestinationBucket = bucket,
                        DestinationKey    = newKey,
                        CannedACL         = cannedAcl
                    });

                    s3.DeleteObject(new DeleteObjectRequest {
                        BucketName = bucket,
                        Key        = obj.Key
                    });
                }
            }
        }
    }

}